
//页面展示
var getPage = function getPage(url) {
    $.ajax({
            type: "get",
            url: url,
            success: function (result) {
                $('#content').html(result);
            },
            error: function () {

            }
        }
    )
}

//全选或全部选
var checkBoxSelectAll = function (that) {
    var tableId = $(that).closest("table").attr("id");
    var tableTbody = $("#" + tableId + " tbody");
    if ($(that).prop("checked")) {
        tableTbody.find("input:checkbox").prop("checked", true);
    } else {
        tableTbody.find("input:checkbox").prop("checked", false);
    }
}

//单选
var checkBoxSelectOne = function (that) {
    var tableId = $(that).closest("table").attr("id");
    var tableTbody = $("#" + tableId + " tbody");
    tableTbody.find("input:checkbox").prop("checked", false);
    $(that).prop("checked", true);
}

//通过分页栏中url属性currentPageNum获取当前页面
var getCurrentPage = function (tableId) {
    return $("#" + getPageId(tableId) + " ul").attr("currentPageNum");
}

//tableId以Table结尾 pageId以P结尾
var getPageId = function (tableId) {
    return tableId.substring(0, tableId.length - 5) + "P";
}

//清除旧数据
var cleanTableAndPage = function (tableId) {
    $("#" + tableId).html("");
    $("#" + getPageId(tableId)).html("");
}

//select CheckBox List
var tableCheckBoxChecked =function (tdList, data) {
    $.each(tdList,function() {
        var that = $(this);
        if (data === that.text()) {
            $(that.closest("tr").find("td")[0]).find("input").prop("checked", true);
        }
    });
}

//pagination 输入框 回车行为
var changePageInput = function(that, functionAuto){
    if(parseInt(that.val())){
        if(parseInt(that.val())>parseInt(that.attr("maxPage"))){
            that.val(that.attr("maxPage"));
        }
        functionAuto(that.val());
    }
}

var showConfirm = function(message, successFunction){
    showDialogueBox({
        'id': 'confirmMsg',
        'type': 'confirm',
        'title': '',
        'message': message,
        'buttons': {
            'Yes': {
                'class': 'btn-primary',
                'action': function () {
                    successFunction();
                }
            },
            'no': {
                'class': 'btn-secondary',
                'action': function () {

                }
            }
        }
    });
}

//弹出错误提示
var showError = function(message){
    showDialogueBox({
        'id': 'errorMsg',
        'type': 'error',
        'title': '错误',
        'message': message
    });
}

//弹出loading
var showLoading = function(){
    showDialogueBox({
        'id': 'loading',
        'type': 'loading',
        'message': "Loading..."
    });
}

//关闭loading
var closeLoading = function() {
    hideDialogueBox("loading");
}

//弹出成功提示
var showSuccess = function(message){
    showDialogueBox({
        'id': 'successMsg',
        'type': 'success',
        'title': "成功",
        'message': message
    });
}

//弹出内部错误对话框
var showInternalError = function(){
    showError("内部错误, 请与管理员联系!");
}

var errorFunction = function(message){
    showError(message);
}

// ajax post
//参数 url 链接 ， postData post使用的数据， successFunction 成功时的回调函数函数
//options errorFunction 返回错误时的回调函数
//        passArguments 成功时回调函数参数
//        errorArguments 失败时回调函数参数
//        successMessage 成功时的提示信息
var postAjax = function (url, postData, successFunction, options) {
    //默认错误返回函数 错误函数参数 正确函数参数
    var defaultOptions = {
        errorFunction:errorFunction,
        successArguments:"",
        errorArguments:"",
        successMessage:""
    }

    var currentOptions = $.extend(defaultOptions,options);
    //TODO remove
    var resultData = "";
    $.ajax({
        type: "post",
        url: url,
        data: postData,
        //beforeSend: function () {
        //    showLoading();
        //},

        success: function (resultData) {
            //closeLoading();
            //if (resultData.result && resultData.result.substr(resultData.result.length - 4) === "0000") {
            if(currentOptions && currentOptions.passArguments){
                if (currentOptions && currentOptions.successArguments) {
                    successFunction(resultData,currentOptions.successArguments,currentOptions.passArguments);
                } else {
                    successFunction(resultData,currentOptions.passArguments);
                }
            }else{
                if (currentOptions && currentOptions.successArguments) {
                    successFunction(resultData,currentOptions.successArguments);
                } else {
                    successFunction(resultData);
                }
            }
            if(currentOptions.successMessage){
                showSuccess(currentOptions.successMessage);
            }

            //} else {
            //    if(currentOptions && currentOptions.errorFunction){
            //        errorFunction(resultData.message);
            //    }else{
            //        if (currentOptions && currentOptions.errorArguments) {
            //            errorFunction(resultData.message, currentOptions.errorArguments.split(','));
            //        } else {
            //
            //        }
            //    }
            //
            //}
        }
        ,
        error: function () {
            //closeLoading();
            showInternalError();
        }
    });
}

var prepareValidateOption = function(){

}

//自动填充表单函数
function loadData(obj){
    var key,value,tagName,type,arr;
    for(x in obj){
        key = x;
        value = obj[x];
        $("[name='"+key+"'],[name='"+key+"[]']").each(function(){
            tagName = $(this)[0].tagName;
            type = $(this).attr('type');
            if(tagName=='INPUT'){
                if(type=='radio'){
                    $(this).attr('checked',$(this).val()==value);
                }else if(type=='checkbox'){
                    arr = value.split(',');
                    for(var i =0;i<arr.length;i++){
                        if($(this).val()==arr[i]){
                            $(this).attr('checked',true);
                            break;
                        }
                    }
                }else{
                    $(this).val(value);
                }
            }else if(tagName=='SELECT' || tagName=='TEXTAREA'){
                $(this).val(value);
            }

        });
    }
}







